//
//  FieldsCellItemView.m
//  RuiHeApp
//
//  Created by Jun on 2022/11/24.
//

#import "FieldsCellItemView.h"
#import "DeviceChartFieldModel.h"

@interface FieldsCellItemView()
@property (nonatomic ,strong) DeviceChartFieldModel *model;

@property (nonatomic ,strong) QMUILabel *titleLab;

@property (nonatomic ,strong) QMUILabel *contentLab;

@property (nonatomic ,strong) UIView *rightLine;

@property (nonatomic ,strong) UIView *bottomLine;

@property (nonatomic ,strong) UIView *bgView;
@end

@implementation FieldsCellItemView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void)setIndex:(NSInteger)index totalCount:(NSInteger)count
{
    BOOL showRightLine = index%3 !=2;
    //有多少行
    NSInteger rowCount = count % 3 ? count/3+1 : count/3;
    //当前第几行
    NSInteger currentRow = index / 3;
    BOOL showBottomLine = count>3 && currentRow != rowCount-1;
    if(index == count-1){
        showRightLine = NO;
    }
    
    self.rightLine.hidden = !showRightLine;
    self.bottomLine.hidden = !showBottomLine;
    
    if(index%3 == 0){
        [self.bottomLine mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.right.mas_offset(0);
        }];
    }else if (index%3 == 1){
        [self.bottomLine mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(0);
            make.right.mas_offset(0);
        }];
        
    }else if (index%3 == 2){
        [self.bottomLine mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(0);
            make.right.mas_offset(-15);
        }];
    }
    
    NSLog(@"---->一共有%ld行 ,当前是第%ld",rowCount,currentRow);
}

- (void)setupUI{
    kWeakSelf;
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = UIColor.whiteColor;
    self.bgView = bgView;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    _titleLab = [[QMUILabel alloc] init];
    [_titleLab setTextAlignment:NSTextAlignmentCenter];
    [_titleLab setFont:[UIFont systemFontOfSize:11]];
    [_titleLab setTextColor:[UIColor colorWithHexString:@"3F4443" alpha:0.6]];
    [_titleLab setNumberOfLines:2];
    [bgView addSubview:_titleLab];
    
    _contentLab = [[QMUILabel alloc] init];
    [_contentLab setTextAlignment:NSTextAlignmentCenter];
    [_contentLab setFont:[UIFont boldSystemFontOfSize:14]];
    [_contentLab setTextColor:FontColor(@"#3F4443")];
    _contentLab.numberOfLines = 3;
    [bgView addSubview:_contentLab];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentLab.mas_bottom).offset(5);
        make.left.mas_offset(5);
        make.right.mas_offset(-5);
        ;
    }];

    [_contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.titleLab);
        make.centerY.mas_offset(-10);
    }];
    
    _rightLine = kView;
    _rightLine.backgroundColor = kCOLOR_HEX(@"E7E7E7");
    [bgView addSubview:_rightLine];
    [_rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(0);
        make.height.offset(kScaleW(30));
        make.centerY.mas_offset(0);
        make.width.offset(1);
    }];
    
    _bottomLine = kView;
    _bottomLine.backgroundColor = kCOLOR_HEX(@"E7E7E7");
    [bgView addSubview:_bottomLine];
    [_bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.bottom.mas_offset(0);
        make.height.offset(1);
    }];
}


- (void)setModel:(DeviceChartFieldModel *)model deviceTypeCode:(NSString*)deviceTypeCode
{
    _model = model;
    if (_model) {
        [self setHidden:NO];
        [_titleLab setText:_model.keyName];
        NSLog(@"---->key = %@ , type = %@, unit = %@",model.keyCode,model.dataType,model.unitName);
        if (ValidStr(_model.content)) {
            
            NSString *content = [NSString stringWithFormat:@"%@ %@",_model.content,ValidStr(_model.unitName)?_model.unitName:@""];
           
            
            if([model.dataType isEqualToString:@"float"]||[model.dataType isEqualToString:@"int"]){
                NSDecimalNumber *value = [NSDecimalNumber decimalNumberWithString:model.content];
                NSString *valueStr = [NSString stringWithFormat:@"%@",value];
                content = [NSString stringWithFormat:@"%@ %@",valueStr,ValidStr(_model.unitName)?_model.unitName:@""];
            }
          
            //枚举类型
            if (_model.isEnum&&_model.enumValues.count>0) {
                 //找出对应下标的枚举值
                 __block NSInteger index = -1;
                 [model.enumIndexs enumerateObjectsUsingBlock:^(NSString*  _Nonnull enumIndex, NSUInteger enumIdx, BOOL * _Nonnull stop) {
                     if ([model.content integerValue] == enumIndex.integerValue && enumIndex.length>0) {
                         index = enumIdx;
                         NSLog(@"--->找到了对应枚举值:%@ ,index = %ld",model.keyName,enumIdx);
                     }
                 }];
                 //找到了就显示对应的枚举值
                 if (index != -1) {
                     NSLog(@"--->找到枚举值:%@",model.keyName);
                     content = [NSString stringWithFormat:@"%@ %@",model.enumValues[index],_model.unitName];
                 }else{
                     //没找到,显示后台返回的值
                     content = [NSString stringWithFormat:@"%@%@",ValidNullData(model.content),_model.unitName];
                     if ([model.keyCode isEqualToString:@"Ems_InterAddr"]) {//逆变器类型显示NULL
                         content = @"NULL";
                     }
                     NSLog(@"--->赋值");
                 }
                    
            }
            //master下的最高最低电压,温度位置
            if ([model.keyCode isEqualToString:@"Mtr_HV_LOC"]||
                [model.keyCode isEqualToString:@"Mtr_LV_LOC"]||
                [model.keyCode isEqualToString:@"Mtr_HT_LOC"]||
                [model.keyCode isEqualToString:@"Mtr_LT_LOC"]||
                [model.keyCode isEqualToString:@"Ems_HV_LOC"]||
                [model.keyCode isEqualToString:@"Ems_LV_LOC"]||
                [model.keyCode isEqualToString:@"Ems_HT_LOC"]||
                [model.keyCode isEqualToString:@"Ems_LT_LOC"]) {
                NSInteger value = [model.content integerValue];
                NSInteger h_loc = value >> 8;
                NSInteger l_loc = value & 0xFF;
                content = [NSString stringWithFormat:@"%ld_%ld",h_loc,l_loc];
                //对于0的处理
                if (value == 0) {
                    content = @"--";
                }
            }
            //233 master参数信息中的最大可充放电功率
            if ([model.keyCode isEqualToString:@"Mtr_MaxChgCur*Mtr_TV"]||
                [model.keyCode isEqualToString:@"Mtr_MaxDischgCur*Mtr_TV"]) {
                NSDecimalNumber *A = [NSDecimalNumber decimalNumberWithString:model.content];
                NSDecimalNumber *B = [NSDecimalNumber decimalNumberWithDouble:1000];
                NSDecimalNumber *result = [A decimalNumberByDividingBy:B];
                NSString *resultString = [result decimalPlacesWithRoundUpWithDigit:3];
                content = [NSString stringWithFormat:@"%@ %@",resultString,ValidStr(_model.unitName)?_model.unitName:@""];
            }
            
            NSLog(@"--->content:%@",content);
            [_contentLab setText:content];
        }else{
            [_contentLab setText:@"--"];
        }
    }else{
        [self setHidden:YES];
    }
    
    //端口正负极温度  S40K主控有这两个
    if([model.keyCode isEqualToString:@"Mtr_DsgPort_P_Tem"]||[model.keyCode isEqualToString:@"Mtr_DsgProt_N_Tem"]){
        if (![deviceTypeCode isEqualToString:@"S40K LV1.0"]) {
            [_contentLab setText:@"NA"];
        }
       
    }
}

@end
